<?php
	include_once 'InterfaceDAO.php';
	include_once "BMetier/Album.class.php";
	include_once 'CategorieDAO.php';
	
	class AlbumDAO {

		public static function store(Album $album) {
			if($album != null) {
				if($album->getId() == null) {
					if($album->getCategorie() != null) {
						 CategorieDAO::store($album->getCategorie());
					}
					$req = "Insert into album set name ='". $album->getName()."', ";
					$req .= "coverage='". $album->getCoverage()."', ";
					$req .= "description='". $album->getDescription()."', ";
					$req .= "categorie=". $album->getCategorie()->getId();
					@mysql_query($req) or die('ERROR : ' . $req);
					$album->setId(intval(mysql_insert_id()));
				}else {
					$req = "Update art set album = ". $art->getArt() ." ";
					$req .= "coverage='". $album->getCoverage()."', ";
					$req .= "description='". $album->getDescription()."', ";
					$req .= "categorie=". $album->getCategorie().getId();
					$req .= "where id = " . $album->getId();
					@mysql_query($req) or die('ERROR : ' . $req);
				}
				return $album;
			}
		}

		public static function delete(Album $album){
			if($album != null) {
				if($album->getId() != null) {
					$req = "Delete From album where id = " . $album->getId();
					@mysql_query($req) or die('ERROR : ' . $req);
				}
			}
		}

		public static function load($id) {
			if($id != null) {
				$req = "Select id, name, coverage, description, categorie From album where id = " . $id;
				$res = @mysql_query($req) or die('ERROR : ' . $req);
				if (mysql_num_rows($res) == 0) {
					return ;
				}

				$album = new Album();
 				while($row = mysql_fetch_assoc($res)){
   					$album->setId(intval($row['id']));
   					$album->setName(strval($row['name']));
   					$album->setDescription(strval($row['description']));
   					$album->setCoverage(strval($row['coverage']));
   					$categorie = CategorieDAO::load(intval($row['categorie']) );
   					$album->setCategorie($categorie);
				}
				return $album;
			}
		}
	}

?>